#-*- encoding=UTF-8 -*-

import unittest
import SkipList


class TestSkipListNode(unittest.TestCase):

	def testSetNext(self):
		n1 = SkipList.SkipList.Node();
		n2 = SkipList.SkipList.Node(10, "Node2");
		n1.setNext(n2);

		self.assertTrue(n1.next == n2);
		self.assertTrue(n2.prev == n1);

		n3 = SkipList.SkipList.Node(5, "Node3");
		n1.setNext(n3);
		self.assertTrue(n1.next == n3 );
		self.assertTrue(n3.next == n2);
		self.assertTrue(n2.next == None);
		self.assertTrue(n2.prev == n3);
		self.assertTrue(n1.prev == None);
		self.assertTrue(n3.prev == n1);

	def testSetAbove(self):
		n1 =  SkipList.SkipList.Node(1, "Node1");
		n2 = SkipList.SkipList.Node(2, "Node2");

		self.assertTrue(n1.above == None);
		self.assertTrue(n1.below == None);
		self.assertTrue(n2.above == None);
		self.assertTrue(n2.below == None);


		n1.setAbove(n2);
		self.assertTrue(n1.above == n2);
		self.assertTrue(n2.below == n1);
		self.assertTrue(n1.below == None);
		self.assertTrue(n2.above == None);

class TestSkipList(unittest.TestCase):
	def setUp(self):
		self.list1 = SkipList.SkipList(2);
		self.list2 = SkipList.SkipList();
		self.list2.insert(5, "node5");
		self.list2.insert(10, "node10");
		self.list2.insert(1, "node1");
		self.list2.insert(7, "node7");


	def _testInsert(self):
		n1 = self.list1.insert(1, "Node1");
		self.assertTrue(n1.value== "Node1");
		self.assertTrue(n1.prev == self.list1.minNode.below);
		self.assertTrue(n1.next == self.list1.maxNode.below);

		n5 = self.list1.insert(5, "Node5");
		self.assertTrue(n5.key == 5);
		self.assertEqual(n5.value, "Node5");
		self.assertTrue(n5.prev == n1);
		self.assertTrue(n5.next == self.list1.maxNode.below);

		n3 = self.list1.insert(3, "Node3");
		self.assertTrue(n3.prev == n1);
		self.assertTrue(n1.next == n3);
		self.assertTrue(n3.next == n5);
		self.assertTrue(n5.prev == n3);

		new_n3 = self.list1.insert(3, "NewNode3");
		self.assertTrue(new_n3 == n3);
		self.assertTrue(new_n3.value  == "NewNode3")
		self.assertTrue(n3.value  == "NewNode3")

		n_min = self.list1.insert(SkipList.MIN_KEY, "MIN_VALUE");
		self.assertTrue(n_min == self.list1.minNode.below);
		self.assertEqual(self.list1.minNode.below.value, "MIN_VALUE");

	def testFind(self):
		flag, node = self.list2.find(10);
		self.assertTrue(flag);
		self.assertEqual(node.value, "node10");

		flag, node7 = self.list2.find(7);
		self.assertTrue(flag);
		self.assertEqual(node7.value, "node7");
		self.assertEqual(node7.key, 7);

		flag, node5 = self.list2.find(6);
		self.assertFalse(flag);
		self.assertTrue(node5.next == node7);
		self.assertEqual(node5.key, 5);
		
		flag, node1 = self.list2.find(0);
		self.assertFalse(flag);
		self.assertEqual(node1.key , SkipList.MIN_KEY );

		flag, node_min = self.list2.find(SkipList.MIN_KEY);
		self.assertTrue(flag);
		self.assertEqual(node_min.value, None);

		flag, node10 = self.list2.find(15);
		self.assertFalse(flag);
		self.assertEqual(node10.key, 10);
		self.assertEqual(node10.value, "node10");

	def testInsertToNext(self):
		flag, node5 = self.list2.find(6);
		self.assertFalse(flag);
		node6 = self.list2.insertToNext(6, "node6", node5);
		self.assertEqual(node6.key, 6);
		self.assertTrue(node6.prev == node5);
		self.assertTrue(node5.next == node6);

		flag, node1 = self.list2.find(1);
		self.assertTrue(flag);
		node3 = self.list2.insertToNext(3, "node3", node1);
		self.assertTrue(node3.prev == node1);
		self.assertTrue(node3.next.key == 5);
		node4 = self.list2.insertToNext(4, "node4", node3);
		self.assertTrue(node4 == node3.next);
		self.assertTrue(node3.next.key == 4);
		self.assertTrue(node3.next.value == "node4");


	def testIsExist(self):
		pass
		#self.list1.insert(1);
		#self.assertTrue(self.list1.isExist(1));
	

if __name__=="__main__":
	unittest.main();


